package com.nguyen.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nguyen.entities.Category;
import com.nguyen.letcook.R;
import com.nguyen.viewholders.CategoryItemViewHolder;

public class ListCategoryAdapter extends BaseAdapter {

	ArrayList<Category> arrCategory = new ArrayList<Category>();
	
	LayoutInflater mInflater;
	public ListCategoryAdapter(Context context){
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		
		return arrCategory.size();
	}

	public void addItem(Category category){
		this.arrCategory.add(category);
	}
	
	@Override
	public Category getItem(int position) {
		
		return arrCategory.get(position);
	}
		
	public int getCategoryId(int position) {
		
		return arrCategory.get(position).getId();
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CategoryItemViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new CategoryItemViewHolder();
			convertView = mInflater.inflate(R.layout.category_item, null);
			viewHolder.image 	= (ImageView)convertView.findViewById(R.id.imgCategory);
			viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
			viewHolder.txtDesc 	= (TextView) convertView.findViewById(R.id.txtDescription);
			
			convertView.setTag(viewHolder);
		}
		else
			viewHolder = (CategoryItemViewHolder)convertView.getTag();
		
		viewHolder.txtTitle.setText(arrCategory.get(position).getTitle());
		viewHolder.txtDesc.setText(arrCategory.get(position).getDescription());
		return convertView;		
	}

}
